#include "stdafx.h"


WorldObject::WorldObject(void) : mLocation(0.0f, 0.0f), mSize(0.0f, 0.0f)
{
}


Coordinates::Location% WorldObject::Location::get(void)
{
    return mLocation;
}


void WorldObject::Location::set(Coordinates::Location% value)
{
    mLocation = value;
}


Coordinates::Size% WorldObject::Size::get(void)
{
    return mSize;
}


void WorldObject::Size::set(Coordinates::Size% value)
{
    mSize = value;
}


HandBanana::Utilities::Orientation WorldObject::Orientation::get(void)
{
    return mOrientation;
}


void WorldObject::Orientation::set(HandBanana::Utilities::Orientation value)
{
    mOrientation = value;
}


Coordinates::Rectangle WorldObject::CollisionRectangle::get(void)
{
    return mCollisionRectangle;
}


void WorldObject::CollisionRectangle::set(Coordinates::Rectangle value)
{
    mCollisionRectangle = value;
}


Coordinates::Rectangle WorldObject::CollisionOffset::get(void)
{
    return mCollisionOffset;
}


void WorldObject::CollisionOffset::set(Coordinates::Rectangle value)
{
    mCollisionOffset = value;
}


HandBanana::Utilities::Actions% WorldObject::Actions::get(void)
{
    return mActions;
}


void WorldObject::AddTrigger(Trigger argTrigger, TriggerHandler^ argHandler)
{
}


void WorldObject::RemoveTrigger(Trigger argTrigger, TriggerHandler^ argHandler)
{
}


void WorldObject::UpdateCollision(void)
{
    mCollisionRectangle = Coordinates::Rectangle(Location.X + CollisionOffset.X, Location.Y + CollisionOffset.Y, CollisionOffset.Width, CollisionOffset.Height);
}


bool WorldObject::Move(Coordinates::Location argVelocity)
{
    if (argVelocity.X == 0 && argVelocity.Y == 0)
        return true;

    // Calculate new Location
    Coordinates::Location newLocation = Coordinates::Location(Location.X + argVelocity.X, Location.Y + argVelocity.Y);

    // Get World
    World^ world = Core::Engine::GetWorld();

    if (newLocation.X < 0)
    {
        newLocation.X = 0;
    } 
    else if (newLocation.X + Size.Width > world->Size.Width)
    {
        newLocation.X = world->Size.Width - Size.Width;
    }

    if (newLocation.Y < 0)
    {
        newLocation.Y = 0;
    } 
    else if (newLocation.Y + Size.Height > world->Size.Height)
    {
        newLocation.Y = world->Size.Height - Size.Height;
    }

    // Calculate collision Rectangle of new Location
    Coordinates::Rectangle newCollisionRectangle = Coordinates::Rectangle(newLocation.X + CollisionOffset.X, newLocation.Y + CollisionOffset.Y, CollisionOffset.Width, CollisionOffset.Height); 

    // Get WorldObjects from Background Layer at new collision rectangle
    List<WorldObject^> objects = world->Layers["Background"]->GetObjects(newCollisionRectangle);

    bool canMove = true;
    for each (WorldObject^ object in objects)
    {
        if ( (object->CollisionRectangle.IsEmpty() == false) && (newCollisionRectangle.Intersects(object->CollisionRectangle) == true) )
        {
            canMove = false;
            break;
        }
    }

    if (canMove)
    {
        // Update Location
        Location = newLocation;
    }

    return canMove;
}